﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DuAnNH.Fd_class;

namespace DuAnNH
{
    public partial class Frm_MA_Update : Form
    {
        public Frm_MA_Update()
        {
            InitializeComponent();
        }
        private void Frm_MA_Update_Load(object sender, EventArgs e)
        {
            Load_CbbLoaiTD();
            Load_DGV();
            Load_LVBan();
        }
        public void Load_CbbLoaiTD()
        {
            DataTable DTCB = new DataTable();
            DTCB = Cls_ThucDon.LayDSMaLoaiThucDon();
            Cbb_LoaiTD.DataSource = DTCB;
            Cbb_LoaiTD.DisplayMember = "TenLoai";
            Cbb_LoaiTD.ValueMember = "MaLoai";
        }
        public void Load_DGV()
        {
            DataTable DT = new DataTable();
            DT = Cls_ThucDon.LayDSThucDon(Cbb_LoaiTD.SelectedValue.ToString());
            DGV_DSTD.DataSource = DT;
        }

        private void Cbb_LoaiTD_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_DGV();
        }
        public void Load_LVBan()
        {
            DataTable tb = new DataTable();
            tb = Cls_Ban.ChonBan(1);
            load_ban(tb);
        }
        public void load_ban(DataTable tb)
        {
            LV_Ban.Items.Clear();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = tb.Rows[i][0].ToString();
                if (tb.Rows[i][2].ToString() == "0")
                    item.ImageIndex = 0;
                else
                {
                    if (tb.Rows[i][2].ToString() == "1")
                        item.ImageIndex = 1;
                }
                LV_Ban.Items.Add(item);
            }
        }
        public void Load_LV_DSTD()
        {
            string MB = LV_Ban.FocusedItem.SubItems[0].Text;
            DataTable DTLV = new DataTable();
            DTLV = Cls_ThucDon.ThucDon_Ban(MB);
            LV_DSGM.Items.Clear();
            for (int i = 0; i < DTLV.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = DTLV.Rows[i][2].ToString();
                item.SubItems.Add(DTLV.Rows[i][3].ToString());
                item.SubItems.Add(DTLV.Rows[i][4].ToString());
                item.SubItems.Add(DTLV.Rows[i][5].ToString());
                item.SubItems.Add(DTLV.Rows[i][6].ToString());
                LV_DSGM.Items.Add(item);
            }
        }

        private void LV_Ban_MouseClick(object sender, MouseEventArgs e)
        {
            Load_LV_DSTD();
            btt_add_td.Enabled = true;
            Btn_Update.Enabled = true;
            aht = 1;
            
        }

        private void btt_add_td_Click(object sender, EventArgs e)
        {
            if (aht == 1)
            {
                int idx = DGV_DSTD.CurrentRow.Index;
                string MaTD = DGV_DSTD.Rows[idx].Cells[0].Value.ToString();
                string mb = LV_Ban.FocusedItem.SubItems[0].Text;
                string TenTD = DGV_DSTD.Rows[idx].Cells[1].Value.ToString();
                string Gia = DGV_DSTD.Rows[idx].Cells[2].Value.ToString();
                int SL = Convert.ToInt32(Txt_SL.Text);
                int g = Convert.ToInt32(Gia);
                int Tien = g * SL;
                //int Tien = Convert.ToInt32(Gia) * Convert.ToInt32(SL);
                if (kt(mb, MaTD) == 0)
                {
                    string str = string.Format("INSERT INTO ChiTietHoaDon VALUES('{0}','{1}',N'{2}','{3}','{4}','{5}')", mb, MaTD, TenTD, Gia, SL, Tien);
                    SqlCommand com = new SqlCommand(str, Coso.con);
                    com.ExecuteNonQuery();
                }
                else
                {
                    if (kt(mb, MaTD) == 1)
                    {
                        string str = string.Format("UPDATE ChiTietHoaDon SET SoLuong='{0}'+SoLuong,Tien='{1}' WHERE MaBan='{2}' AND MaTD='{3}'", SL, Tien, mb, MaTD);
                        SqlCommand com = new SqlCommand(str, Coso.con);
                        com.ExecuteNonQuery();
                    }
                }
                Load_LV_DSTD();
            }
        }
        public int kt(string MB, string MaTD)
        {
            string sql = string.Format("select count(*) from ChiTietHoaDon where MaBan='{0}' AND MaTD='{1}'", MB, MaTD);
            SqlCommand sqlcmd = new SqlCommand(sql, Coso.con);
            return (int)sqlcmd.ExecuteScalar();
        }
        private void Btn_Update_Click(object sender, EventArgs e)
        {
            string MB = LV_Ban.FocusedItem.SubItems[0].Text;
            for (int i = 0; i < LV_DSGM.Items.Count; i++)
            {
                string MaTD = LV_DSGM.Items[i].SubItems[0].Text;
                string TenTD = LV_DSGM.Items[i].SubItems[1].Text;
                string Gia = LV_DSGM.Items[i].SubItems[2].Text;
                string SL = LV_DSGM.Items[i].SubItems[3].Text;
                string Tien = LV_DSGM.Items[i].SubItems[4].Text;
                if (kt(MB, MaTD) == 0)
                {
                    string str = string.Format("INSERT INTO ChiTietHoaDon VALUES('{0}','{1}',N'{2}','{3}','{4}','{5}')", MB, MaTD, TenTD, Gia, SL, Tien);
                    SqlCommand com = new SqlCommand(str, Coso.con);
                    com.ExecuteNonQuery();
                    //if (Cls_ThucDon.Update_CTHD1(mb, MaTD, TenTD, Gia, SL, Tien))
                    //{
                    //}
                }
                if (kt(MB, MaTD) == 1)
                {
                    string str = string.Format("UPDATE ChiTietHoaDon SET SoLuong='{0}',Tien='{1}' WHERE MaBan='{2}' AND MaTD='{3}'", SL, Tien, MB, MaTD);
                    SqlCommand com = new SqlCommand(str, Coso.con);
                    com.ExecuteNonQuery();
                    //if (Cls_ThucDon.Update_CTHD(mb, MaTD, SL, Tien))
                    //{
                    //}
                }
            }
        }

        private void Txt_SL_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        int ahx = 0;
        int aht = 0;
        private void btt_del_Click(object sender, EventArgs e)
        {
            if (ahx == 1)
            {
                string mb = LV_Ban.FocusedItem.SubItems[0].Text;
                for (int i = 0; i < LV_DSGM.Items.Count; i++)
                {
                    string MaTD = LV_DSGM.FocusedItem.SubItems[0].Text;
                    string str = string.Format("DELETE From ChiTietHoaDon WHERE MaBan='{0}' AND MaTD='{1}'", mb, MaTD);
                    SqlCommand com = new SqlCommand(str, Coso.con);
                    com.ExecuteNonQuery();
                }
                Load_LV_DSTD();
            }
            ahx = 0;
        }

        private void DGV_DSTD_MouseClick(object sender, MouseEventArgs e)
        {
            ahx = 0;
            //aht = 1;
        }

        private void LV_DSGM_MouseClick(object sender, MouseEventArgs e)
        {
            ahx = 1;
            aht = 0;
        }
        
    }
}
